/*
 * @Descripttion: 
 * @version: 
 * @Author: kl2018
 * @Date: 2020-04-30 17:24:44
 * @LastEditors: kl2018
 * @LastEditTime: 2020-06-04 16:35:38
 */
import { observable, action, toJS } from "mobx";
import { SearchAdvanced } from 'weaver-mobile-page';
import { Tools, Toast } from "weaver-mobile";

class SearchStore {
  @observable search = new SearchAdvanced.Store({ route: "" });
  @observable searchAdValue = "";
  @observable url = ""
  @observable searchHotTerm = {};
  @observable conditions = []

  @action.bound
  setSearchPath(path) {
    this.search = new SearchAdvanced.Store({ route: path })
  }

  @action.bound
  getSearchCondition(param) {
    const params = { url: "", method: 'POST', params: {}, ...param }
    this.getData(params)
  }

  // 关联高级搜索与前端搜索条件
  @action.bound
  updateAdSearchSubject = (value, key) => {
    this.searchAdValue = value;
    this.search.form.updateField(key, value);
  }

  //高级搜索 重置方法
  @action.bound
  searchFormReset() {
    this.searchAdValue = "";
    this.search.form.resetFields();
    this.search.form.initForm(toJS(this.conditions), this.search.path);
  }

  //高级搜索 搜索栏和高级搜索的内容一致
  @action.bound
  searchFormChange(condition, value, key) {
    if (condition.domkey[0] === key) {
      this.searchAdValue = value;
    }
    this.search.form.updateField(condition.domkey[0], value);
  }

  @action.bound
  setSearchValue(value) {
    this.searchAdValue = value;
  }

  @action.bound
  getData = (params) => new Promise((resolve, reject) => {
    try {
      Tools.callApi(params).then(action((res) => {
        const conditions = res[this.responseKey] || res.conditions || res.condition;
        this.conditions = conditions;
        if (conditions[0].searchHotTerm) {
          this.searchHotTerm = conditions[0].searchHotTerm;
        }

        this.search.form.initForm(conditions, this.search.path);
        this.search.conditions = conditions;
        resolve(this.search.form);
      }));
    } catch (e) {
      reject(e);
      Toast.info(e, 1);
    }
  })

}

export default SearchStore;
